﻿
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using NFLManager.Data;
using NFLManager.Properties;

namespace NFLManager
{
    public class Controller
    {

        protected static Controller _controller = null;
        private static ProgressForm _progressForm = ProgressForm.Istance;

        private static List<Team> teams = new List<Team>();

        public static Controller Istance
        {
            get { return _controller ?? (_controller = new Controller()); }
        }


        public DataTable Roster(string team)
        {
            var html = Fetch.Istance.Roster(team);
            return DataManager.Istance.Players(html);
        }

        public DataTable Players()
        {
            var result = DataManager.Istance.EmptyPlayers;
            var Teams = DataManager.Istance.GetTeamsFromDB();
            foreach (var team in teams) result.Merge(Roster(team.name));
            return result;
        }

        public ProgressForm ProgressForm
        {
            get { return _progressForm; }
        }

        

        public void ActivateProgress( int steps)
        {
            _progressForm.SetProgressBar(steps, 1);
            _progressForm.StartTimer();
            _progressForm.Visible = true;
            _progressForm.Show();
        }

        public void AddProgress()
        {
            _progressForm.AddProgress();
        }

        public static void RestoreDB(string season)
        {
            RestoreTeamsTable();
            RestoreMatchsTable(season);
            RestorePlayersTable();
            RestoreInjuriesTable();
        }

        public static void RestoreTeamsTable()
        {
            var dtTeams = DataManager.Istance.Teams();
            DataManager.WriteTeamsTableToDB(dtTeams);
        }

        public static void RestoreMatchsTable(string season)
        {
            var dtMatchs = DataManager.Istance.Matchs(season);
            DataManager.WriteMatchsTableToDB(dtMatchs);
        }

        public static void RestorePlayersTable()
        {
            var dtPlayers = DataManager.Istance.Players();
            DataManager.WritePlayersTableToDB(dtPlayers);
        }

        public static void RestoreInjuriesTable()
        {
            var dtInjuries = DataManager.Istance.Injuries();
            DataManager.WriteInjuriesTableToDB(dtInjuries);
        }

        public static void RestoreLeadersTable()
        {
            var dtLeaders = DataManager.Istance.Leaders();
            DataManager.WriteLeadersTableToDB(dtLeaders);
        }
    }
}
